package server;

import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

public class ServerInitializer {

	public static void main(String[] args) {
		
		ServerSocket serverSocket=null;
		IServerMediator mediator=new ServerMediator();
		
		
		try {
			serverSocket=new ServerSocket(50999);
		} catch (IOException e) {
			System.err.println("Errore nell'apertura del Server");
			System.exit(1);
		}
		
		System.out.println("Server inizialiazzato correttamente...");
		
		while(true){
			try {
				Socket socket = serverSocket.accept();
				
				InetAddress infoclient = socket.getInetAddress();
				String c = infoclient.getHostAddress();
				int portclient = socket.getLocalPort();
				
				System.out.println("Il client " + c + " " + portclient
						+ " si è collegato...");
				
				
				
				Client client=new Client(socket, mediator);
				Thread thread=new Thread(client);
				mediator.addClient(client);
				thread.start();
				
			
				
			} catch (IOException e) {
				System.out.println("Errore nell'apertura di un Thread");
				
			}
			
		}
		
	}
}
